home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // CtlDemo5.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "Resource.h"
- #include "CtlDemo5.h"
-
- #define IDC_RED 100
- #define IDC_GREEN 101
- #define IDC_BLUE 102
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_CREATE ()
- ON_BN_CLICKED (IDC_RED, OnRedButtonClicked)
- ON_BN_CLICKED (IDC_GREEN, OnGreenButtonClicked)
- ON_BN_CLICKED (IDC_BLUE, OnBlueButtonClicked)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- CString strWndClass = AfxRegisterWndClass (
- 0,
- myApp.LoadStandardCursor (IDC_ARROW),
- (HBRUSH) (COLOR_3DFACE + 1),
- myApp.LoadStandardIcon (IDI_APPLICATION)
- );
-
- Create (strWndClass, "CtlDemo5");
- }
-
- int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
- {
- if (CFrameWnd::OnCreate (lpcs) == -1)
- return -1;
-
- CClientDC dc (this);
- int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
-
- m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
-
- CFont* pOldFont = dc.SelectObject (&m_font);
- TEXTMETRIC tm;
- dc.GetTextMetrics (&tm);
- m_cxChar = tm.tmAveCharWidth;
- m_cyChar = tm.tmHeight + tm.tmExternalLeading;
- dc.SelectObject (pOldFont);
-
- m_ctlGroupBox1.Create ("Sample text", WS_CHILD | WS_VISIBLE |
- BS_GROUPBOX, CRect (m_cxChar * 2, m_cyChar, m_cxChar * 62,
- m_cyChar * 8), this, UINT (-1));
-
- m_ctlText.Create ("Click a button to change my color",
- WS_CHILD | WS_VISIBLE | SS_CENTER, CRect (m_cxChar * 4,
- m_cyChar * 4, m_cxChar * 60, m_cyChar * 6), this);
-
- m_ctlGroupBox2.Create ("Color", WS_CHILD | WS_VISIBLE |
- BS_GROUPBOX, CRect (m_cxChar * 64, m_cyChar, m_cxChar * 80,
- m_cyChar * 8), this, UINT (-1));
-
- m_ctlRadioButtonRed.Create ("Red", WS_CHILD | WS_VISIBLE | WS_GROUP |
- BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 3,
- m_cxChar * 78, m_cyChar * 4), this, IDC_RED);
-
- m_ctlRadioButtonGreen.Create ("Green", WS_CHILD | WS_VISIBLE |
- BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, (m_cyChar * 9) / 2,
- m_cxChar * 78, (m_cyChar * 11) / 2), this, IDC_GREEN);
-
- m_ctlRadioButtonBlue.Create ("Blue", WS_CHILD | WS_VISIBLE |
- BS_AUTORADIOBUTTON, CRect (m_cxChar * 66, m_cyChar * 6,
- m_cxChar * 78, m_cyChar * 7), this, IDC_BLUE);
-
- m_ctlRadioButtonRed.SetCheck (1);
- m_ctlText.SetTextColor (RGB (255, 0, 0));
-
- m_ctlGroupBox1.SetFont (&m_font, FALSE);
- m_ctlGroupBox2.SetFont (&m_font, FALSE);
- m_ctlRadioButtonRed.SetFont (&m_font, FALSE);
- m_ctlRadioButtonGreen.SetFont (&m_font, FALSE);
- m_ctlRadioButtonBlue.SetFont (&m_font, FALSE);
- return 0;
- }
-
- void CMainWindow::OnRedButtonClicked ()
- {
- m_ctlText.SetTextColor (RGB (255, 0, 0));
- }
-
- void CMainWindow::OnGreenButtonClicked ()
- {
- m_ctlText.SetTextColor (RGB (0, 255, 0));
- }
-
- void CMainWindow::OnBlueButtonClicked ()
- {
- m_ctlText.SetTextColor (RGB (0, 0, 255));
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CColorStatic message map and member functions
-
- BEGIN_MESSAGE_MAP (CColorStatic, CStatic)
- ON_WM_CTLCOLOR_REFLECT ()
- END_MESSAGE_MAP ()
-
- CColorStatic::CColorStatic ()
- {
- m_crTextColor = RGB (0, 0, 0);
- m_crBkColor = ::GetSysColor (COLOR_3DFACE);
- m_brBkgnd.CreateSolidBrush (m_crBkColor);
- }
-
- void CColorStatic::SetTextColor (COLORREF crColor)
- {
- m_crTextColor = crColor;
- Invalidate ();
- }
-
- void CColorStatic::SetBkColor (COLORREF crColor)
- {
- m_crBkColor = crColor;
- m_brBkgnd.DeleteObject ();
- m_brBkgnd.CreateSolidBrush (crColor);
- Invalidate ();
- }
-
- HBRUSH CColorStatic::CtlColor (CDC* pDC, UINT nCtlColor)
- {
- pDC->SetTextColor (m_crTextColor);
- pDC->SetBkColor (m_crBkColor);
- return (HBRUSH) m_brBkgnd;
- }
-